import 'package:flutter/material.dart';
import 'package:whale/helper/global/user/logic.dart';

import '../../helper/utils/util.dart';
import 'index.dart';
import 'models/me.dart';

class MeController extends GetxController {
  MeController();

  final state = MeState();

  bool goLogin() {
    if (UserController().isLogin) {
      return false;
    }
    Get.toNamed(RouterPath.login)!.then((value) {
      update();
    });
    return true;
  }

  void goFocus() {
    if (goLogin()) {
      return;
    }
  }

  void goUpPage() {
    if (goLogin()) {
      return;
    }
    Get.toNamed(RouterPath.helpPage);
  }

  void goCollect() {
    if (goLogin()) {
      return;
    }
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    setupList();
    setupMenu();
  }

  void goMeInfo() {
    if (goLogin()) {
      return;
    }
    gotoSetting();
  }

  void gotoSetting() async {
    bool b = await Get.toNamed(RouterPath.setup);
    if (b) {
      update();
    }
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  void setupList() {
    state.list = [
      MeModel(
        name: '最近浏览',
        icon: 'icon_list1.png'.imgPathMe,
        color: const Color(0xffFA8303),
        bgColor: const Color(0xffFFF3E8),
        onTap: () {
          if (goLogin()) {
            return;
          }
        },
      ),
      MeModel(
        name: '意见反馈',
        icon: 'icon_list2.png'.imgPathMe,
        color: const Color(0xff6D95FF),
        bgColor: const Color(0xffEEF6FE),
        onTap: () => goUpPage(),
      ),
      MeModel(
        name: '联系我们',
        icon: 'icon_list3.png'.imgPathMe,
        color: const Color(0xffFED361),
        bgColor: const Color(0xffFFFAEE),
        onTap: () {
          if (goLogin()) {
            return;
          }
          Get.toNamed(RouterPath.about_us);
        },
      ),
    ];
  }

  void setupMenu() {
    state.menus = [
      MeIconModel(
        name: '我的上传',
        url: 'icon_bg1.png'.imgPathMe,
        textColor: const Color(0xff286CDB),
        onTap: () {
          if (goLogin()) {
            return;
          }
          Get.toNamed(RouterPath.myUploadPage);
        },
      ),
      MeIconModel(
        name: '我的收藏',
        url: 'icon_bg2.png'.imgPathMe,
        textColor: const Color(0xffE07F2E),
        onTap: () {},
      ),
      MeIconModel(
        name: '我的定制',
        url: 'icon_bg3.png'.imgPathMe,
        textColor: const Color(0xff24B2B9),
        onTap: () {},
      ),
    ];
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
